package com.taotao.portal.service;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.pojo.ItemDesc;
import com.taotao.portal.ext.bean.Item;
import com.taotao.portal.httpclientService.HttpClientService;

@Service
public class ItemService {

	@Autowired
	private HttpClientService httpService;
	
	@Value("${MANAGE_URL}")
	private String MANAGE_URL;
	
	@Value("${SSO_BASE_URL}")
	public String SSO_BASE_URL;
	
	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	public Item queryItemById(Long itemId)  {
		String url = MANAGE_URL + "/rest/item/" +itemId;
		
		try {
			String data = httpService.doGet(url);
			if(StringUtils.isNotEmpty(data)){
				return MAPPER.readValue(data, Item.class);
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	public ItemDesc queryDescByItemId(Long itemId) {
		String url = MANAGE_URL + "/rest/item/desc" + itemId;
		
		try {
			String data = httpService.doGet(url);
			if(StringUtils.isNoneEmpty(data)){
				return MAPPER.readValue(data, ItemDesc.class);
			}
			return null;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String queryParamByItemId(Long itemId) {
		// TODO Auto-generated method stub
		 return null;
	}

}
